import { BeServo } from '../../../../model/parts/Servo'
import { AppModel } from '../../../../model/AppModel'
import { createPartMenu } from '../BlockPackage'

export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model:AppModel) {
        this.appendDummyInput()
            .appendField("舵机")
            .appendField(createPartMenu(model, BeServo), 'servo')
            .appendField("转动到位置°")
        this.appendValueInput("pos")
    } ,

    run (args: any, util: any, runtime: any, model:AppModel) {
    } ,

    coder: {
        JavaScript(block: any, coder: any, model: any) {

            // 
            let partUUID = block.getFieldValue('servo')
            let part = model.findPartFromUUID(partUUID)
            if(!part)
                return "// unknow Servo part uuid: " + partUUID

            let val = coder.valueToCode(block, 'pos', coder.ORDER_ASSIGNMENT) || '0'

            return `${coder.indentString()}be.part["${part.instanceName}"].setValue(${val})\n`
        }
    }
}
